package com.jrzh.mvc.controller.sys.admin;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jrzh.framework.I18nHelper;
import com.jrzh.framework.annotation.NoPermission;
import com.jrzh.framework.annotation.RequiredPermission;
import com.jrzh.framework.annotation.UserEvent;
import com.jrzh.framework.base.controller.BaseAdminController;
import com.jrzh.framework.base.search.BaseSearch;
import com.jrzh.framework.bean.JqGridTree;
import com.jrzh.framework.bean.ResultBean;
import com.jrzh.framework.bean.SessionUser;
import com.jrzh.framework.contants.GeneralConstant;
import com.jrzh.mvc.constants.SysActionConstants;
import com.jrzh.mvc.constants.SysConstant.RESOURCE_TYPE;
import com.jrzh.mvc.convert.sys.ResourcesConvert;
import com.jrzh.mvc.model.sys.ResourcesModel;
import com.jrzh.mvc.model.sys.RoleModel;
import com.jrzh.mvc.model.sys.UserModel;
import com.jrzh.mvc.search.sys.ResourcesSearch;
import com.jrzh.mvc.service.sys.manage.SysServiceManage;
import com.jrzh.mvc.view.sys.ResourcesView;

@RestController(ResourcesController.LOCATION + "/ResourcesController")
@RequestMapping(ResourcesController.LOCATION)
public class ResourcesController extends BaseAdminController {
	
	public static final String LOCATION = "sys/admin/resource";
	
	@Autowired
	SysServiceManage sysServiceManage;
	
	@Autowired
	ResourcesConvert resourcesConvert;

	@PostMapping(value = "treegrid")
	@UserEvent(desc = "资源列表查询")
	@RequiredPermission(key = SysActionConstants.INDEX)
	public JqGridTree treegrid(ResourcesSearch search) {
		JqGridTree tree = new JqGridTree();
		try {
			tree = sysServiceManage.resourcesService.treegrid(search, getSessionUser());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return tree;
	}
	
	@PostMapping(value = "changeStatus/{id}")
	@UserEvent(desc = "改变资源禁用/启用状态")
	@RequiredPermission(key = SysActionConstants.DISABLE_ENABLE)
	public ResultBean changeStatus(@PathVariable("id") String id, ResourcesView view){
		ResultBean result = new ResultBean();
		try {
			view.setId(id);
			sysServiceManage.resourcesService.editByFields(view, new String[]{"isDisable"}, getSessionUser());
			result.setStatus(ResultBean.SUCCESS);
			result.setMsg(I18nHelper.getI18nByKey("message.operation_success", getSessionUser()));
		}catch (Exception ex) {
			ex.printStackTrace();
			result.setMsg(ex.getMessage());
		}
		return result;
	}
	
	@PostMapping(value = "menuList")
	@NoPermission
	public ResultBean menuList(ResourcesSearch search) {
		ResultBean result = new ResultBean();
		List<ResourcesView> resourceList = new ArrayList<ResourcesView>();
		try {
			Boolean isAll = false;
			search.setOrder(BaseSearch.Order_Type_Asc);
			search.setSort("sortNum");
			search.setEqualIsDisable(false);
			if(StringUtils.isNotBlank(request.getParameter("isAll"))){
				isAll = true;
			}
			SessionUser sessionUser = getSessionUser();
			List<ResourcesModel> modelList = new ArrayList<ResourcesModel>();
			UserModel user = sysServiceManage.userService.findByField("code", sessionUser.getCode());
			if(StringUtils.equals(user.getCode(), GeneralConstant.USER_CODE_CONSTANT.SUPER_ADMIN_CODE)){
				if(!isAll){
					search.setInTypes(new Integer[]{RESOURCE_TYPE.MENU, RESOURCE_TYPE.FUNC});
				}
				modelList = sysServiceManage.resourcesService.list(search);        
			}else{
				List<RoleModel> roleList = user.getRoles();
				if(null != roleList && roleList.size() > 0){
					for(RoleModel role : roleList){
						List<ResourcesModel> resourcesList = role.getResources();
						if(null != resourcesList && resourcesList.size() > 0){
							for(ResourcesModel resource : resourcesList){
								if(isAll || !resource.getType().equals(RESOURCE_TYPE.ACTION)){
									modelList.add(resource);
								}
							}
						}
					}
				}
			}
			resourceList = resourcesConvert.convertToViewListI18n(modelList, sessionUser);
			result.setObjs(new Object[]{resourceList});
			result.setStatus(ResultBean.SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg(e.getMessage());
			result.setStatus(ResultBean.ERROR);
		}
		return result;
	}
	
	@Override
	protected void setData() {
		
	}
}
